﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ruandao.MiniFace
{
    public abstract class DetailsWidgetBase : WidgetBase
    {
        public DetailsWidgetBase()
        {
        }

        public override void Render( TemplateContext templateContext, System.IO.TextWriter writer)
        {
            dynamic dataObject;
            var vars = VariablesToPython(templateContext, out dataObject);
            for (int i = 0; i < vars.Count; i++)
            {
                templateContext.PythonScope.SetVariable(vars[i].Key, vars[i].Value);
            }

            foreach (var content in this.WidgetTemplate.Contents)
            {
                content.Render(templateContext, writer);
            }

            for (int i = vars.Count-1; i >= 0; i--)
            {
                templateContext.PythonScope.RemoveVariable(vars[i].Key);
            }
        }

        /// <summary>
        /// 获取暴露给Python脚本的所有对象的集合，Value为所暴露的对象，对应的Key为在Python中引用该对象的变量名。
        /// </summary>
        /// <param name="templateContext"></param>
        /// <returns></returns>
        protected abstract List<KeyValuePair<string,dynamic>> VariablesToPython(TemplateContext templateContext, out dynamic dataObject);

    }
}
